package GUI;

import javax.swing.JPanel;
import java.awt.SystemColor;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import Controler.*;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
/**
  * @author Lucie
 * Class name  Lend_book_panel
 * extend  Abstract_Jpanel
 * purpose: this panel get copy number and member id from the user and call the controller 
 * to add the lend 
 */
public class Lend_book_panel extends Abstract_Jpanel {
	private JTextField textcopynum;
	private JTextField textid;

	/**
	 * Create the panel.
	 */
	public Lend_book_panel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(20, 48, 669, 297);
		add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(65, 40, 492, 183);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		textcopynum = new JTextField();
		textcopynum.setBounds(187, 36, 128, 20);
		panel_1.add(textcopynum);
		textcopynum.setColumns(10);
		
		JLabel lblCopyNumber = new JLabel("copy number:");
		lblCopyNumber.setBounds(70, 29, 88, 30);
		panel_1.add(lblCopyNumber);
		lblCopyNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textid = new JTextField();
		textid.setBounds(187, 78, 128, 20);
		panel_1.add(textid);
		textid.setColumns(10);
		
		JLabel lblMemberId = new JLabel("member ID:");
		lblMemberId.setBounds(70, 75, 84, 23);
		panel_1.add(lblMemberId);
		lblMemberId.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JButton btnGo = new JButton("lend");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Lend_Book_controler lend_controler=new Lend_Book_controler();
				if(lend_controler.get_data(get_copy_id(),get_memmber_id()))
				{
					next_screen_success();
					empty_text_fields();
					
				}
				else {
					next_screen_faild(lend_controler.get_msg());
					
				}
	
				
			}
		});
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGo.setBounds(576, 367, 92, 33);
		add(btnGo);
		
		JLabel lblLendABook = new JLabel("Lend an Item");
		lblLendABook.setForeground(new Color(25, 25, 112));
		lblLendABook.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblLendABook.setBounds(41, 11, 275, 24);
		add(lblLendABook);

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"apply success");
	}
	/**
	 * this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"apply faild "+msg);
	}
	/**
	 * 
	 * @return copy id
	 */
	public String get_copy_id()
	{
		return textcopynum.getText();
	}
	/**
	 * 
	 * @return member id
	 */
	public String get_memmber_id()
	{
		return  textid.getText();
	}
	/**
	 * this method set all text fields to be empty
	 */
	public void empty_text_fields()
	{
		textid.setText(null);
		textcopynum.setText(null);
		
	
	}
}
